package com.example.myapplication.until;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import com.example.myapplication.R;

import java.util.Random;

public class ImageUtils {

    private static Bitmap background;

    private static Bitmap landImg;

    private static Bitmap[] birdImg = new Bitmap[4] ;

    private static Bitmap dangongImg;

    private static Bitmap manfulImg ;

    private static Bitmap autoImg ;

    private static Bitmap beginImg ;

    private static Bitmap endImg ;

    private static Bitmap[] pigImg = new Bitmap[3];

    private static Bitmap[] woodDestory = new Bitmap[15];

    public static void init(Activity a) {

        beginImg = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.begin),Globals.PIECE_WIDTH,Globals.PIECE_HEIGH) ;

        endImg = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.end),Globals.PIECE_WIDTH,Globals.PIECE_HEIGH) ;

        background = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.main_bg),Globals.SCREEN_WIDTH * 2,
                Globals.SCREEN_HEIGH * 2);

        landImg = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.ground),Globals.SCREEN_WIDTH,Globals.PIECE_HEIGH);

        birdImg[0] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.bird_0),Globals.PIECE_WIDTH,Globals.PIECE_HEIGH);

        birdImg[1] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.bird_1),Globals.PIECE_WIDTH,Globals.PIECE_HEIGH);

        birdImg[2] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.bird_2),Globals.PIECE_WIDTH,Globals.PIECE_HEIGH);

        birdImg[3] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.bird_3),Globals.PIECE_WIDTH,Globals.PIECE_HEIGH);

        dangongImg = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.dangong),Globals.PIECE_WIDTH / 2,
                Globals.PIECE_HEIGH * 3);

        pigImg[0] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.pig1),1.5f*Globals.PIECE_WIDTH ,
                1.5f*Globals.PIECE_HEIGH);

        pigImg[1] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.pig2),1.5f*Globals.PIECE_WIDTH ,
                1.5f*Globals.PIECE_HEIGH);

        pigImg[2] = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.pig3),1.5f*Globals.PIECE_WIDTH ,
                1.5f*Globals.PIECE_HEIGH);

        manfulImg = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.manful),Globals.PIECE_WIDTH ,Globals.PIECE_HEIGH);

        autoImg = changeSize(BitmapFactory.decodeResource(a.getResources(),
                R.drawable.atuo),Globals.PIECE_WIDTH ,Globals.PIECE_HEIGH);
    }

    public static Bitmap getBeginImg() {
        return beginImg;
    }

    public static Bitmap getEndImg() {
        return endImg;
    }

    public static int getPigIndex() {
        Random random = new Random() ;
        int index = random.nextInt(12);
        return index%3 ;
    }

    public static Bitmap getAutoImg() {
        return autoImg;
    }

    public static Bitmap getManfulImg() {
        return manfulImg;
    }

    public static Bitmap getPigImg(int index){
        return pigImg[index] ;
    }

    public static Bitmap getBirdImg(int index) {
        return birdImg[index];
    }

    private static Bitmap changeSize(Bitmap source, float overWidth,
                                     float overHeight) {
        Matrix m = new Matrix();
        m.postScale(overWidth / source.getWidth(),
                overHeight / source.getHeight());

        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), m, false);
    }

    public static Bitmap getBackground() {
        return background;
    }

    public static Bitmap getLandImg() {
        return landImg;
    }

    public static Bitmap getDangongImg() {
        return dangongImg;
    }

}